# Copyright (c) 2022 by Apex.AI Inc. All rights reserved.
# Copyright (c) 2022 by NXP. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
load("//bazel:configure_file.bzl", "configure_file")
load("//bazel:configure_version.bzl", "configure_version")

#
########## posh core lib ##########
#
configure_file(
    name = "iceoryx_posh_deployment_hpp",
    src = "cmake/iceoryx_posh_deployment.hpp.in",
    out = "include/iceoryx_posh/iceoryx_posh_deployment.hpp",
    config = select({
        # FIXME: for values see "iceoryx_posh/cmake/IceoryxPoshDeployment.cmake" ... for now some nice defaults
        "@platforms//os:macos": {
            "IOX_COMMUNICATION_POLICY": "ManyToManyPolicy",
            "IOX_INTERNAL_MAX_NUMBER_OF_NOTIFIERS": "256",
            "IOX_MAX_CHUNKS_ALLOCATED_PER_PUBLISHER_SIMULTANEOUSLY": "8",
            "IOX_MAX_CHUNKS_HELD_PER_SUBSCRIBER_SIMULTANEOUSLY": "256",
            "IOX_MAX_CLIENTS_PER_SERVER": "256",
            "IOX_MAX_ID_STRING_LENGTH": "100",
            "IOX_MAX_INTERFACE_NUMBER": "4",
            "IOX_MAX_NODE_NAME_LENGTH": "100",
            "IOX_MAX_NODE_NUMBER": "1000",
            "IOX_MAX_NODE_PER_PROCESS": "50",
            "IOX_MAX_NUMBER_OF_CONDITION_VARIABLES": "1024",
            "IOX_MAX_NUMBER_OF_MEMPOOLS": "32",
            "IOX_MAX_PROCESS_NUMBER": "300",
            "IOX_MAX_PUBLISHERS": "512",
            "IOX_MAX_PUBLISHER_HISTORY": "16",
            "IOX_MAX_REQUEST_QUEUE_CAPACITY": "1024",
            "IOX_MAX_RESPONSES_PROCESSED_SIMULTANEOUSLY": "16",
            "IOX_MAX_RESPONSE_QUEUE_CAPACITY": "16",
            "IOX_MAX_RUNTIME_NAME_LENGTH": "98",
            "IOX_MAX_SHM_SEGMENTS": "100",
            "IOX_MAX_SUBSCRIBERS": "1024",
            "IOX_MAX_SUBSCRIBERS_PER_PUBLISHER": "256",
        },
        "//conditions:default": {
            "IOX_COMMUNICATION_POLICY": "ManyToManyPolicy",
            "IOX_INTERNAL_MAX_NUMBER_OF_NOTIFIERS": "256",
            "IOX_MAX_CHUNKS_ALLOCATED_PER_PUBLISHER_SIMULTANEOUSLY": "8",
            "IOX_MAX_CHUNKS_HELD_PER_SUBSCRIBER_SIMULTANEOUSLY": "256",
            "IOX_MAX_CLIENTS_PER_SERVER": "256",
            "IOX_MAX_ID_STRING_LENGTH": "100",
            "IOX_MAX_INTERFACE_NUMBER": "4",
            "IOX_MAX_NODE_NAME_LENGTH": "100",
            "IOX_MAX_NODE_NUMBER": "1000",
            "IOX_MAX_NODE_PER_PROCESS": "50",
            "IOX_MAX_NUMBER_OF_CONDITION_VARIABLES": "1024",
            "IOX_MAX_NUMBER_OF_MEMPOOLS": "32",
            "IOX_MAX_PROCESS_NUMBER": "300",
            "IOX_MAX_PUBLISHERS": "512",
            "IOX_MAX_PUBLISHER_HISTORY": "16",
            "IOX_MAX_REQUEST_QUEUE_CAPACITY": "1024",
            "IOX_MAX_RESPONSES_PROCESSED_SIMULTANEOUSLY": "16",
            "IOX_MAX_RESPONSE_QUEUE_CAPACITY": "16",
            "IOX_MAX_RUNTIME_NAME_LENGTH": "100",
            "IOX_MAX_SHM_SEGMENTS": "100",
            "IOX_MAX_SUBSCRIBERS": "1024",
            "IOX_MAX_SUBSCRIBERS_PER_PUBLISHER": "256",
        },
    }),
)

configure_version(
    name = "iceoryx_versions_hpp",
    src = "cmake/iceoryx_versions.hpp.in",
    out = "include/iceoryx_versions.hpp",
    version_from = "//:VERSION",
)

#
########## posh lib ##########
#

# Special file handling - part 1: Files which are part of "iceoryx_posh" (despite located in "roudi"-subdirectory)
iceory_posh_extra_roudi_files = [
    "source/roudi/service_registry.cpp",
]

# Special file handling - part 2: Files which are part of "iceoryx_posh_config"
iceory_posh_config_files = [
    "source/gateway/gateway_config.cpp",
    "source/gateway/toml_gateway_config_parser.cpp",
    "source/roudi/roudi_config_toml_file_provider.cpp",
]

# Special file handling - part 3: Files which are part of "iox-roudi" executable
iox_roudi_executable_files = [
    "source/roudi/application/roudi_main.cpp",
]

cc_library(
    name = "iceoryx_posh",
    srcs = glob(
               [
                   "source/log/**",
                   "source/capro/**",
                   "source/error_handling/**",
                   "source/mepoo/**",
                   "source/popo/**",
                   "source/version/**",
                   "source/runtime/**",
               ],
               exclude = iceory_posh_config_files,
           ) +
           iceory_posh_extra_roudi_files,
    hdrs = glob(["include/**"]) + [
        ":iceoryx_posh_deployment_hpp",
        ":iceoryx_versions_hpp",
    ],
    strip_include_prefix = "include",
    visibility = ["//visibility:public"],
    deps = [
        "//iceoryx_dust",
        "//iceoryx_hoofs",
    ],
)

#
########## posh lib for gateway support ##########
#
cc_library(
    name = "iceoryx_posh_gateway",
    srcs = glob(
        ["source/gateway/**"],
        exclude = iceory_posh_config_files,
    ),
    strip_include_prefix = "include",
    visibility = ["//visibility:public"],
    deps = [":iceoryx_posh"],
)

#
########## posh roudi lib ##########
#

cc_library(
    name = "iceoryx_posh_roudi",
    srcs = glob(
        ["source/roudi/**"],
        exclude = iceory_posh_extra_roudi_files + iceory_posh_config_files + iox_roudi_executable_files,
    ),
    strip_include_prefix = "include",
    visibility = ["//visibility:public"],
    deps = [":iceoryx_posh"],
)

#
######### posh config lib ##########
#
cc_library(
    name = "iceoryx_posh_config",
    srcs = iceory_posh_config_files,
    strip_include_prefix = "include",
    visibility = ["//visibility:public"],
    deps = [
        ":iceoryx_posh",
        "@cpptoml",
    ],
)

#
######### posh roudi daemon ##########
#
cc_binary(
    name = "iox-roudi",
    srcs = iox_roudi_executable_files,
    visibility = ["//visibility:public"],
    deps = [
        ":iceoryx_posh_config",
        ":iceoryx_posh_roudi",
    ],
)

#
########## build iceoryx posh testing lib ##########
#
cc_library(
    name = "iceoryx_posh_testing",
    srcs = glob(["testing/**/*.cpp"]),
    hdrs = glob(["testing/include/**"]),
    strip_include_prefix = "testing/include",
    visibility = ["//visibility:public"],
    deps = [
        ":iceoryx_posh",
        ":iceoryx_posh_roudi",
        "//iceoryx_hoofs:iceoryx_hoofs_testing",
        "@googletest//:gtest",
    ],
)
